// pages/index/amap.js

const app = getApp()
import {
  MCHLocation
} from '../../api/amap'
import {
  getInfo
} from '../../api/mch'

import {
  LBSLocation
} from '../../api/amap'

import {
  lockSysUser
} from '../../api/member'

const util = require('../../utils/util.js')
const chooseLocation = requirePlugin('chooseLocation');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    building: '请开启定位功能',
    address: '需要您的位置信息,安排最近配送站点',
    latitude: 0,
    longitude: 0,
    buttonName:'正在获取您的位置',
    shopList:[],
    sysMch:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this;
    const location = chooseLocation.getLocation();
    
     console.info("地图选点回来了" + JSON.stringify(location))
    if (location != null) {
      this.data.latitude = location.latitude;
      this.data.longitude = location.longitude
      this.setData({
        latitude: location.latitude,
        longitude: location.longitude,
        building: location.name,
        address: location.address
      })
      _this.data.buttonName = "修改收货地址"
          _this.setData({
            buttonName:_this.data.buttonName
          })
      _this.startMCHLocation(location.latitude,location.longitude)

    } else {
      _this.data.buttonName = "正在获取您的位置"
      _this.setData({
        buttonName:_this.data.buttonName
      })
      wx.getLocation({
        type: 'gcj02',
        success (res) {
          console.log(res)
          const latitude = res.latitude
          const longitude = res.longitude
          _this.data.buttonName = "修改收货地址"
          _this.setData({
            buttonName:_this.data.buttonName
          })
          _this.data.latitude = res.latitude;
          _this.data.longitude = res.longitude
          _this.startLBSLocation(latitude,longitude)
          _this.startMCHLocation(latitude,longitude)
        },
        fail() {
          _this.data.buttonName = "选择收货地址"
          _this.setData({
            buttonName:_this.data.buttonName
          })
        }
       })
  
    }
  },

  startLBSLocation(lat,lng) {
    var _this = this;
    LBSLocation({lat:lat,lng:lng}).then(res=>{
      console.log(res)
      _this.setData({
        latitude:_this.data.latitude,
        longitude:_this.data.longitude,
        building: res.data.name,
        address: res.data.address
      })
     }
   )
  },

  startMCHLocation:function (lat,lng) {
    var _this = this;
    MCHLocation({isvId:app.ISV_ID,lat:lat,lng:lng}).then(res=>{
       console.log(res)
       if (res.data!=null) {
        //  app.globalData.mchId = res.data.sysMch.id
        //  app.globalData.mchName = res.data.sysMch.name
         //app.globalData.location = true
         _this.data.sysMch = res.data.sysMch
         res.data.shop.distance = res.data.shop.distance.toFixed(2)
         _this.data.shopList = []
         _this.data.shopList.push(res.data.shop)
       }
       _this.setData({
         shopList:_this.data.shopList
       })
      }
    )
 },
 
 chooseLocation: function () {
   const key = app.globalData.mapKey; //使用在腾讯位置服务申请的key
   const referer = '位置选择'; //调用插件的app的名称
   
   wx.navigateTo({
     url: `plugin://chooseLocation/index?key=${key}&referer=${referer}`
   });
 },
 startLockSysUser() {
  lockSysUser({mchId:app.globalData.mchId,xuid:app.globalData.xuid,shopId:app.globalData.shopId}).then(res=>{
    console.log(res)
  })
},
 closePage: function (e) {
   console.log(e);
   app.globalData.shopId = e.currentTarget.dataset.shopid;
   app.globalData.shopName = e.currentTarget.dataset.shopname;
   app.globalData.mchId = this.data.sysMch.id
   app.globalData.mchName = this.data.sysMch.name
   app.globalData.location = true

   if (app.globalData.token!=null) {
     this.startLockSysUser()
   }

   wx.navigateBack({
     delta:1,
     complete: (res) => {},
   })
 }

})